﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EvaluationObjectCalculator
{
    class ComputeAndOutupt
    {
        /// <summary>
        /// 计算最终结果，并且写入到dataGridView1中
        /// </summary>
        /// <param name="alCompany">投标单位集合</param>
        /// <param name="alPrice">投标价格集合</param>
        /// <param name="dataGridView1">要写入到的DataGridView表</param>
        /// <param name="start">去掉最高评标价的个数</param>
        /// <param name="end">去掉最低评标价的个数</param> 
        public void Compute(ArrayList alCompany, List<double> alPrice,DataGridView dataGridView1,TextBox mtb1, TextBox mtb2, TextBox ntb1, TextBox ntb2, TextBox ntb3, TextBox ntb4, int start,int end,ref int dgvCount)
        {
            double A1 = 0; //报价第一次算术平均值：A1 
            double jiZhunA1BiLv = 0; //与基准价A1相比比率
            double pingBiaoJia2 = 0; //评标参与第二次计算报价A3
            double pingBiaojunJia2 = 0; //报价第二次算术平均值：A2
            double pingBiaoJia2Sum = 0; //评标参与第二次计算报价的和值
            int pingBiaoJia2_count = 0; //评标参与第二次计算报价的数量 
            double pingBiaojunJia3 = 0; //报价第三次算术平均值：A3
            double jiZhunPianChaA3 = 0; //与基准价A3偏差率

            double m = 0;
            double n = 0;
            double deFen = 0; //投标价得分
            List<double> pingBiaoJia2Validity = new List<double>(); //评标参与第二次计算报价有效值
            List<double> deFengSort = new List<double>(); //得分集合
            bool isIn_20_10 = true; //与基准价A1相比比率是否在-20~10之间

            if (ntb3.Text.Trim().Length==0)
            {
                MessageBox.Show("权重比不能为空", "错误提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            else if(!double.TryParse(ntb3.Text.Trim().ToString(),out double d))
            {
                MessageBox.Show("权重比输入错误", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //获得行数
            int rowCount = 0;
            while (dataGridView1.Rows[rowCount].Cells[0].Value != null)
            {
                rowCount++;
            }

            Comput cb = new Comput();
            cb.Sort(alPrice, alCompany); //对投标单位和价格排序，以价格从大到小排序，

            A1 = cb.avgArrsyList(alPrice, start, end); //报价第一次算术平均值：A1
            for (int i = 0; i < alPrice.Count; i++)
            {
                jiZhunA1BiLv = cb.A1BiLv(alPrice[i], A1);//与基准价A1相比比率
                pingBiaoJia2 = cb.PingBiaoJia2(alPrice[i], jiZhunA1BiLv, out isIn_20_10); //评标参与第二次计算报价A2
                if (isIn_20_10)
                {
                    pingBiaoJia2Sum += pingBiaoJia2; //评标参与第二次计算报价的和值
                    pingBiaoJia2_count++;//评标参与第二次计算报价的数量 
                }

                //写入到dataGridView1中
                dataGridView1.Rows[i].Cells[0].Value = alCompany[i];
                dataGridView1.Rows[i].Cells[1].Value = alPrice[i];
                //dataGridView1.Rows[i].Cells[2].Value = jiZhunA1BiLv;

                ////插入评标参与第二次计算报价
                //if (pingBiaoJia2 == 0)
                //{
                //    dataGridView1.Rows[i].Cells[3].Value = "";
                //}
                //else
                //{
                //    dataGridView1.Rows[i].Cells[3].Value = pingBiaoJia2;
                //}

                //将pingBiaoJia2为非0的数据写入pingBiaoJia2Validity集合中
                if (pingBiaoJia2 > 0)
                {
                    pingBiaoJia2Validity.Add(pingBiaoJia2);
                }
            }
            pingBiaojunJia2 = pingBiaoJia2Sum / pingBiaoJia2_count; //报价第二次算术平均值：A2
            pingBiaoJia2Validity.Sort();
            pingBiaojunJia3 = (pingBiaojunJia2 + Convert.ToDouble(pingBiaoJia2Validity[0])) / 2; //报价第三次算术平均值：A3

            //计算得分
            for (int i = 0; i < alPrice.Count; i++)
            {
                jiZhunPianChaA3 = Math.Round(100 * (Convert.ToDouble(alPrice[i]) - pingBiaojunJia3) / pingBiaojunJia3, 2);
                //dataGridView1.Rows[i].Cells[4].Value = jiZhunPianChaA3.ToString() + "%"; //插入与基准价A3偏差率

                //计算mn的值
                cb.ComputeMN(Convert.ToDouble(alPrice[i]), pingBiaojunJia3, mtb1,mtb2,ntb1,ntb2, out m, out n);

                //计算得分
                deFen = Math.Round(100 - 100 * 1 * m * Math.Abs(alPrice[i] - pingBiaojunJia3) / pingBiaojunJia3, 4);
                if (deFen < 0)
                {
                    deFen = 0;
                }
                dataGridView1.Rows[i].Cells[2].Value = deFen; //将投标价得分插入到dataGridView1中
                dataGridView1.Rows[i].Cells[5].Value =Math.Round(deFen*Convert.ToDouble(ntb3.Text.ToString().Trim())/100,4); //将投权重得分插入到dataGridView1中
                deFengSort.Add(deFen); //将投标价得分插入到结集合deFengSort中
            }

            //计算排名和与第一名的分差，并写入到dataGridView1中
            cb.Ranking(deFengSort, dataGridView1, ntb4, 2, rowCount,out dgvCount);

        }
    }
}
